package com.zhufeng.gateway.db.config;

import com.alibaba.fastjson.JSONObject;
import com.zhufeng.user.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: GateWayConfig
 * @Description TODO
 * @author 月夜烛峰
 * @date 2022/9/16 14:51
 */
@Slf4j
//@Configuration
public class GateWayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        // 构建多个路由routes
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

//        routes.route("zhufeng-route-msg",
//                r -> r.readBody(UserInfo.class, requestBody -> {
//                            log.info("requestBody is {}", requestBody);
//                            return true;
//                        }).and().path("/msg/**").
//
//                        uri("lb://zhufeng-web-msg"));
        routes.route("zhufeng-route-msg", r -> r.path("/json").and().readBody(JSONObject.class, requestBody -> {
            log.info("requestBody is {}", requestBody);
            return true;
        }).and().method("POST").filters(f -> f.prefixPath("/msg/")).uri("lb://zhufeng-web-msg"));
        //routes.route("zhufeng-route-msg1", r -> r.path("/json").and().method("POST").filters(f -> f.prefixPath("/msg/")).uri("lb://zhufeng-web-msg"));
        //routes.route("zhufeng-route-user", r -> r.path("/user/**").and().method("GET").uri("lb://zhufeng-web-user"));

        // 返回所有路由规则
        return routes.build();
    }
}
